// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

// 云函数入口函数
exports.main = async (event, context) => {
  const db = cloud.database()
  const wxContext = cloud.getWXContext()
  
  try {
    const result = await db.collection('accountDetail').add({
      data: {
        _openid: wxContext.OPENID || event.openid,
        accountId: event.accountId,
        date: event.date,
        type: event.type,
        amount: event.amount,
        remark: event.remark,
        createTime: db.serverDate(),
        updateTime: db.serverDate()
      }
    })
    return {
      code: 0,
      data: result,
      message: '保存成功'
    }
  } catch (err) {
    return {
      code: -1,
      error: err,
      message: '保存失败'
    }
  }
}